<?php
namespace gzapi\payment\controller;

use cmf\controller\PublicController;
use think\Validate;
use gzapi\common\business\Payment;
use think\Db;
class CompanyController extends PublicController
{
    //方法验证参数
    protected $auth = '*';
    protected $Payment;
    protected $training_status = [
        '创建' => 1,
        '编辑' => 2,
        '提交' => 3,
    ];
    protected function initialize()
    {
        $this->Payment = new Payment;
    }
    //查看培训列表
    public function paymentList()
    {
        $where['azd_id'] = ['=' , input('param.azd_id')];
        $where['created_by'] = ['=' , $this->user['mobilphone']];
        $data = $this->Payment->paymentList($where);
        $this->success("查询成功!", ['data' => $data]);
    }
//    //删除
//    public function delpayment(){
//        $where['row_id'] =input('post.row_id');
//        $detail=Db::table('cmf_gz_hise_service_pro_payment_detail')->where('train_id',input('post.row_id'))->find();
//        if($detail){
//             $this->error('该培训已有详细数据无法删除');
//        }
//        $res=Db::table('cmf_gz_hise_service_pro_payment')->where($where)->delete();
//        if($res){
//            $this->success('删除成功');
//        }
//    }
//    //获取安装商信息
//    public function trainGetFws()
//    {
//        $where['fwfgs'] = ['=',$this->user['bm_code']];
//        $data = $this->Payment->paymentGetFws($where);
//        $this->success("查询成功!", ['data' => $data]);
//    }

    //新建培训
    public function paymentAdd()
    {
        $get_data = input('param.');
        if(!isset($get_data['training_time']) || !$get_data['training_time'])
        {
            $this->error('时间错误');
        }
        if(!isset($get_data['azd_id']) || !$get_data['azd_id'])
        {
            $this->error('标识错误');
        }
        if(!isset($get_data['installation_unit']) || !$get_data['installation_unit'])
        {
            $this->error('受训单位不能为空');
        }
        if(!isset($get_data['training_content']) || !$get_data['training_content'])
        {
            $this->error('培训内容不能为空');
        }
        //判断这个安装单号是不是这个分公司的
        $where['fwfgs'] = ['=',$this->user['bm_code']];
        $where['azd_id'] = ['=',$get_data['azd_id']];

        $data['training_time'] = $get_data['training_time'];
        $data['azd_id'] = $get_data['azd_id'];
        $data['installation_unit'] = $get_data['installation_unit'];
        $data['training_content'] = $get_data['training_content'];
        $data['created_by'] = $this->user['mobilphone'];
        $data['trainer'] = $this->user['user_name'];
        $data['training_no'] = $this->paymenting_no();
        $data['training_status'] = $this->training_status['创建'];
        $data['created_date'] = date('Y-m-d',time());

        $result = $this->Payment->paymentAdd($where,$data);
        if($result['code'])
        {
            $this->success("创建成功!");
        }else{
            $this->error($result['msg']);
        }

        
    }

    //生成培训编号
    public function paymenting_no()
    {
        $str = $this->user['bm_code'] . date('YmdHis',time()) . rand(100000,999999);
        return $str;
    }

    //进入培训详情(编辑培训)
    public function paymentEdit()
    {
        $where['created_by'] = ['=',$this->user['mobilphone']];
        $where['row_id'] = ['=',input('param.row_id')];
        $data = $this->Payment->paymentDetails($where);
        if($data['tp_teacher'])
        {
            $data['tp_teacher'] = [$data['tp_teacher']];
        }else{
            $data['tp_teacher'] = [];
        }

        if($data['tp_scene'])
        {
            $data['tp_scene'] = [$data['tp_scene']];
        }else{
            $data['tp_scene'] = [];
        }

        if($data['tp_trainee'])
        {
            $data['tp_trainee'] = [$data['tp_trainee']];
        }else{
            $data['tp_trainee'] = [];
        }
        $this->success("查询成功!",['data'=>$data]);
    }

    //暂时保存培训信息
    public function paymentSave()
    {
        $get_data = input('param.');
        if(isset($get_data['tp_teacher']))
        {
            $data['tp_teacher'] = $get_data['tp_teacher'];
        }
        if(isset($get_data['tp_scene']))
        {
            $data['tp_scene'] = $get_data['tp_scene'];
        }
        if(isset($get_data['tp_trainee']))
        {
            $data['tp_trainee'] = $get_data['tp_trainee'];
        }
        if(isset($get_data['training_content']))
        {
            $data['training_content'] = $get_data['training_content'];
        }
        if(!$get_data['tp_teacher']){
             $this->error('缺少讲师图片');
        }
        if(!$get_data['tp_scene']){
            $this->error('缺少全景图片');
       }
       if(!$get_data['tp_trainee']){
        $this->error('缺少培训人员图片');
   }
       
        $data['training_status'] = $this->training_status['编辑'];
        $data['last_upd_date'] = date('Y-m-d',time());
        $where['row_id'] = ['=',$get_data['row_id']];
        $where['created_by'] = ['=',$this->user['mobilphone']];
        $data['last_upd_by'] = ['=',$this->user['mobilphone']];
        $result = $this->Payment->paymentSave($where,$data);
        $this->success("保存成功!");
        // if($result['code'])
        // {
        //     $this->success("保存成功!");
        // }else{
        //     $this->error($result['msg']);
        // }
    }


    //提交培训信息
    public function paymentSubmit()
    {
        $get_data = input('param.');
        if(isset($get_data['tp_teacher']))
        {
            $data['tp_teacher'] = $get_data['tp_teacher'];
        }
        if(isset($get_data['tp_scene']))
        {
            $data['tp_scene'] = $get_data['tp_scene'];
        }
        if(isset($get_data['tp_trainee']))
        {
            $data['tp_trainee'] = $get_data['tp_trainee'];
        }
        if(isset($get_data['training_content']))
        {
            $data['training_content'] = $get_data['training_content'];
        }
        $data['training_status'] = $this->training_status['提交'];
        $data['last_upd_date'] = date('Y-m-d',time());
        $where['row_id'] = ['=',$get_data['row_id']];
        $where['created_by'] = ['=',$this->user['mobilphone']];
        $data['last_upd_by'] = ['=',$this->user['mobilphone']];
        //检查是否有图片
        $train_pic=Db::table('cmf_gz_hise_service_pro_payment')->where(['row_id'=>$get_data['row_id']])->find();
        if(!$data['tp_teacher']){
            $this->error('讲师照片还未上传');
        }
        if(!$data['tp_scene']){
            $this->error('全景照片还未上传');
        }
        if(!$data['tp_trainee']){
            $this->error('培训人员还未上传');
        }
        //检查培训人数
        $train_num=Db::table('cmf_gz_hise_service_pro_payment_detail')->where(['train_no'=>$train_pic['training_no']])->count();
        if($train_num<1){
            $this->error('请先人员签到');
        }
        $result = $this->Payment->paymentSave($where,$data);
        if($result['code'])
        {
            $this->success("保存成功!");
        }else{
            $this->error($result['msg']);
        }
    }
    public function delpayment(){
        $where['row_id']=input('post.row_id');
        $deres=Db::table('cmf_gz_hise_service_pro_payment_detail')->where('train_id',$where['row_id'])->find();
        if($deres){
            $this->error('该培训已有详细数据，无法删除');
        }
        $data = Db::table('cmf_gz_hise_service_pro_payment')->where($where)->find();
        if($data['tp_teacher'] || $data['tp_scene'] || $data['tp_trainee'])
        {
            $this->error('该培训已有照片，无法删除');
        }
        $res= Db::table('cmf_gz_hise_service_pro_payment')->where($where)->delete();
        if($res){
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    //删除培训信息（暂时不做，后面如果增加删除，删除此段注释）
    public function trainDel()
    {

    }

    //创建二维码
    public function _trainCode()
    {

    }














}
